package com.rzico.sbl.ui.report_0537

import androidx.core.view.isVisible
import com.jeremyliao.liveeventbus.LiveEventBus
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportOrderConfirmQfBinding
import com.rzico.sbl.ui.report.ReportOrderDetailActivity
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.ActivityStack
import com.xinnuo.common.helper.doubleValue
import com.xinnuo.common.other.DecimalNumberFilter
import com.xinnuo.common.other.NameLengthFilter
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.utils.bindLifeCycle
import com.xinnuo.common_ui.utils.clear
import com.xinnuo.common_ui.utils.showListDialog
import com.xinnuo.common_ui.utils.subscribeBySuccess

class QFOrderConfirmActivity : BaseActivity(R.layout.activity_report_order_confirm_qf) {

    private val mBinding by lazy { ActivityReportOrderConfirmQfBinding.bind(getRootView()) }

    private var mPayAmount = ""
    private var mPayMethod = ""

    override fun getViewModel() = get<QFReportOrderViewModel>()

    override fun initData() {
        initTitle(title = "确认订单")
    }

    override fun initListener() {
        mBinding.apply {
            mPayAmount = intent.getExtra("amount")
            intent.also {
                confirmTotal.text = mPayAmount
                confirmNum.setRightString(it.getExtra("orderSn"))
                confirmTime.setRightString(it.getExtra("time"))
            }

            confirmReal.filters = arrayOf(DecimalNumberFilter(), NameLengthFilter(8))
            confirmOwe.filters = arrayOf(DecimalNumberFilter(), NameLengthFilter(8))

            confirmPay.oneClick {
                showListDialog(
                    title = "请选择付款方式",
                    items = listOf("微信", "支付宝", "现金", "记账"),
                    listener = { _, hint ->
                        mPayMethod = when (hint) {
                            "微信" -> {
                                confirmOwe.clear()
                                confirmReal.setText(mPayAmount.formatDecimal())
                                "weixin"
                            }
                            "支付宝" -> {
                                confirmOwe.clear()
                                confirmReal.setText(mPayAmount.formatDecimal())
                                "alipay"
                            }
                            "现金" -> {
                                confirmOwe.clear()
                                confirmReal.setText(mPayAmount.formatDecimal())
                                "cash"
                            }
                            "记账" -> {
                                confirmReal.clear()
                                confirmOwe.setText(mPayAmount.formatDecimal())
                                "monthly"
                            }
                            else -> ""
                        }

                        confirmRealLl.isVisible =
                            mPayMethod.isNotEmpty() && mPayMethod != "monthly"
                        confirmPay.setRightString(hint)
                    }
                )
            }

            btConfirm.oneClick {
                when {
                    mPayMethod.isEmpty() -> {
                        showToast("请选择付款方式")
                        return@oneClick
                    }
                    confirmRealLl.isVisible
                            && confirmReal.content.isEmpty() -> {
                        showToast("请输入实付金额")
                        return@oneClick
                    }
                    mPayMethod == "monthly"
                            && confirmOwe.content.isEmpty() -> {
                        showToast("请输入记账金额")
                        return@oneClick
                    }
                    confirmRealLl.isVisible
                            && (confirmReal.textDouble + confirmOwe.textDouble) > mPayAmount.doubleValue -> {
                        showToast("实付、记账总金额不能大于应收金额")
                        return@oneClick
                    }
                    mPayMethod == "monthly"
                            && confirmOwe.textDouble > mPayAmount.doubleValue -> {
                        showToast("记账金额不能大于应收金额")
                        return@oneClick
                    }
                }

                getViewModel()
                    .confirmOrder(
                        orderSn = intent.getExtra("orderSn"),
                        method = mPayMethod,
                        paid = confirmReal.content,
                        unpaid = confirmOwe.content,
                        memo = confirmMemo.textTrim
                    )
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeBySuccess(
                        event = getViewModel(),
                        onSuccess = {
                            showToast("订单确认成功！")

                            // 报货订单刷新
                            LiveEventBus
                                .get("orderModify")
                                .post("confirm")

                            ActivityStack
                                .screenManager
                                .popActivities(
                                    ReportOrderDetailActivity::class.java,
                                    QFOrderConfirmActivity::class.java
                                )
                        }
                    )
            }
        }
    }

}